﻿<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
protected string Notice = "";
protected SqlDataReader Reader;
protected SqlConnection Conn;
protected SqlCommand Cmd;
protected string PinMing = "";
protected string GuiGe = "";
protected string GongHuo = "";
protected DateTime RiQi = DateTime.Today;
protected string FenChang = "";
protected int KuQu = 0;
protected int ZhiShu = 0;
protected decimal BiaoZhong = 0;
protected decimal BangZhong = 0;
protected string CheHao = "";
protected string BeiZhu = "";
protected int Rows = 0;
        
protected void Page_Load(object sender, EventArgs e)
{
    string list = Request.QueryString["list"];
    if (list == null || list=="")
    {
        Response.End();
        return;
    }
    string[] tmp=list.Split(new char[]{','});
    int t;
    for (int i = 0; i < tmp.Length; i++)
    {
        if(!int.TryParse(tmp[i], out t))
        {
            Response.End();
            return;
        }
        else if (t <= 0)
        {
            Response.End();
            return;
        }
    }

    Conn = new SqlConnection();
    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
    Conn.Open();
    Cmd = new SqlCommand();
    Cmd.Connection = Conn;

    Cmd.CommandText =
@"select case when t1.分厂=0 then ''
when t1.分厂=1 then '一分厂'
when t1.分厂=2 then '二分厂'
when t1.分厂=3 then '三分厂'
when t1.分厂=4 then '四分厂'
when t1.分厂=5 then '五分厂'
when t1.分厂=-1 then '酸洗'
else '未知分厂'
end as 分厂
,t1.库区
,t1.车号
,t1.日期
,t1.磅重
,t1.标重
,t1.支数
,t1.明细
,t1.备注
,case when t1.原料ID=1 then '油管光管(J55)'
when t1.原料ID=2 then '套管光管(J55)'
when t1.原料ID=3 then '油管接箍光管(J55)'
when t1.原料ID=4 then '套管接箍光管(J55)'
when t1.原料ID=5 then '成品油管接箍(J55)'
when t1.原料ID=6 then '成品套管接箍(J55)'
when t1.原料ID=101 then '油管光管(N80Q)'
when t1.原料ID=102 then '套管光管(N80Q)'
when t1.原料ID=103 then '油管接箍光管(N80Q)'
when t1.原料ID=104 then '套管接箍光管(N80Q)'
when t1.原料ID=105 then '成品油管接箍(N80Q)'
when t1.原料ID=106 then '成品套管接箍(N80Q)'
else '其他原料' end as 原料
,case when t1.原料ID=1 then '73*5.51'
when t1.原料ID=2 then '139.7*7.72'
when t1.原料ID=3 then '88.9*12.5'
when t1.原料ID=4 then '153.67*13.5'
when t1.原料ID=5 then '88.9*12.5'
when t1.原料ID=6 then '5-1/2 LC'
when t1.原料ID=101 then '73*5.51'
when t1.原料ID=102 then '139.7*7.72'
when t1.原料ID=103 then '88.9*12.5'
when t1.原料ID=104 then '153.67*13.5'
when t1.原料ID=105 then '88.9*12.5'
when t1.原料ID=106 then '153.67*13.5'
else '---' end as 规格
,t2.名称 as 供货单位
from 原料到货记录 t1
left outer join 原料供货单位 t2 on t2.ID=t1.供货ID where t1.id in(" + list + ")";

    Cmd.CommandType = CommandType.Text;
    Reader = Cmd.ExecuteReader();
    while (Reader.Read())
    {
        if (this.PinMing == "")
        {
            this.PinMing = (string)Reader["原料"];
            this.GuiGe = (string)Reader["规格"];
            this.GongHuo = (string)Reader["供货单位"];
            this.RiQi = (DateTime)Reader["日期"];
            this.FenChang = (string)Reader["分厂"];
            this.KuQu = (int)Reader["库区"];
            this.ZhiShu = (int)Reader["支数"];
            this.BiaoZhong = (decimal)Reader["标重"];
            this.BangZhong = (decimal)Reader["磅重"];
            this.CheHao = (string)Reader["车号"];
            this.BeiZhu = (string)Reader["备注"];
        }
        else
        {
            this.ZhiShu+= (int)Reader["支数"];
            this.BiaoZhong += (decimal)Reader["标重"];
            this.BangZhong += (decimal)Reader["磅重"];
            this.CheHao += " "+(string)Reader["车号"];
            this.BeiZhu += " "+(string)Reader["备注"];
        }
    }
    Reader.Close();
    Cmd.CommandText = "select rtrim(炉号) as 炉号,rtrim(批次) as 批次,sum(重量) as 重量,sum(支数) as 支数 from 原料到发明细 where 记录ID in("+list+") group by 炉号,批次;";
    Reader = Cmd.ExecuteReader();

}
</script>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript" language="javascript" src="/common.js"></script>
<form id="form1" runat="server" onsubmit="return checkinput();">
<span id="wintop" class="wintop" style="display:none;">原料发货明细</span>
<asp:Panel ID="Panel1" runat="server" CssClass="panel">

<input type="button" value="导出Excel" class="button" onclick="Export();" />

<asp:Button ID="Button_Close" runat="server" Text="关 闭" UseSubmitBehavior="false"  Visible="true"
        CssClass="button" OnClientClick="parent.swin2_close();return false;" />

</asp:Panel>

<style>
select{font-size:12pt;background-color:#d2e8ae;}
td.bb{border:0.5pt solid black;text-align:center;height:30px;}
</style>
<center>
<script>putBillHead();</script>
<asp:Panel ID="Panel3" runat="server" CssClass="panel3">
<table id=mt border=0  cellpadding=0 style='border-collapse: collapse;font-size:12pt; font-family:宋体;'>
<tr>
<td colspan=12 style='height:45px;font-size:22pt;font-weight:bold;text-align:center;'>管 材 到 (发) 货 登 记 表</td>
</tr>
<tr>
<td colspan=4>重量单位：吨</td>
<td colspan=4></td>
<td colspan=4>编码：SYC/ZJ-WL-07</td>
</tr>
<tr>
<td class=bb colspan=3>品名(钢级)</td>
<td class=bb colspan=3>规格</td>
<td class=bb colspan=3>供货单位</td>
<td class=bb colspan=3>到货日期</td>
</tr>
<tr>
<td class=bb colspan=3><%=this.PinMing %></td>
<td class=bb colspan=3><%=this.GuiGe %></td>
<td class=bb colspan=3><%=this.GongHuo %></td>
<td class=bb colspan=3><%=this.RiQi.ToLongDateString() %></td>
</tr>

<tr>
<td class=bb colspan=3>使用单位</td>
<td class=bb colspan=2>存储库区</td>
<td class=bb colspan=2>支数</td>
<td class=bb colspan=2>质保书量</td>
<td class=bb colspan=3>磅重</td>
</tr>
<tr>
<td class=bb colspan=3><%=this.FenChang %></td>
<td class=bb colspan=2><%=this.KuQu %></td>
<td class=bb colspan=2><%=this.ZhiShu %></td>
<td class=bb colspan=2><%=this.BiaoZhong %></td>
<td class=bb colspan=3><%=this.BangZhong %></td>
</tr>

<tr>
<td class=bb colspan=2>车皮号：</td>
<td class=bb colspan=10 style="text-align:left;"><%=this.CheHao %></td>
</tr>

<tr>
<td class=bb colspan=2>备　注：</td>
<td class=bb colspan=10 style="text-align:left;"><%=this.BeiZhu %></td>
</tr>
<tr>
<td class=bb style="border-top:2.5pt double black;"></td>
<td class=bb colspan=3 style="border-top:2.5pt double black;">炉号</td>
<td class=bb colspan=3 style="border-top:2.5pt double black;">批次</td>
<td class=bb colspan=2 style="border-top:2.5pt double black;">支数</td>
<td class=bb colspan=3 style="border-top:2.5pt double black;">重量</td>
</tr>
<%
    while (Reader.Read())
    {
        this.Rows++;
        Response.Write("<tr><td class=bb>" + Rows + "</td><td class=bb colspan=3>"+(string)Reader["炉号"]
            +"</td><td class=bb colspan=3>"+(string)Reader["批次"]
            +"</td><td class=bb colspan=2>"+(int)Reader["支数"]
            +"</td><td class=bb colspan=3>"+ Convert.ToDouble((decimal)Reader["重量"])+"</td></tr>");
    }
    Reader.Close();
    Reader.Dispose();
    Conn.Close();
    Cmd.Dispose();
    Conn.Dispose();
%>
<script>
    for (var i = <%=this.Rows+1 %>; i <= 20; i++) {
        document.write("<tr><td class=bb>" + i + "</td><td class=bb colspan=3></td><td class=bb colspan=3></td><td class=bb colspan=2></td><td class=bb colspan=3></td></tr>");
    }
</script>
<%
    if (this.Rows < 20) this.Rows = 20;    
%>
<tr>
<td width=50></td><td width=50></td><td width=50></td>
<td width=50></td><td width=50></td><td width=50></td>
<td width=50></td><td width=50></td><td width=50></td>
<td width=50></td><td width=50></td><td width=50></td>
</tr>
</table>
</asp:Panel>
<script>putBillFoot();</script>
</center>
</form>
<form id="form2" method="post" action="/PageExport.ashx">
<input type="hidden" name="filename" value="管材发货登记表.xls" />
<input type="hidden" name="content" value="" />
</form>
<script>
    var notice = "<%=this.Notice %>";
    if (notice != "") {
        parent.showmsgbox(notice);
    }
</script>
<script type="text/javascript">


    //导出Excel
    function Export() {
        var code = document.getElementById("Panel3").innerHTML;

        code = code.replace(/<\/TBODY>/i, "<tr><td colspan=12 height=30>物料中心留存</td></tr></TBODY>") + code.replace(/<\/TBODY>/i, "<tr><td colspan=12 height=30>分厂留存</td></tr></TBODY>");

        code = code.replace(/\n/img, "");
        code = code.replace(/<\/TBODY>.*<TBODY>/img, "");
        
        //删除空行
        code = code.replace(/<TR[^>]*>([\r\n]+<TD[^>]*><\/TD>)+<\/TR>/img, "");

        form2.content.value =
"<html xmlns:o='urn:schemas-microsoft-com:office:office'\nxmlns:x='urn:schemas-microsoft-com:office:excel'\nxmlns='http://www.w3.org/TR/REC-html40'>\n<head>\n<meta http-equiv=Content-Type content='text/html; charset=utf-8'>\n<style>\n<!--table\n@page\n{mso-header-data:;\nmso-footer-data:;\nmargin:.79in .31in .59in .31in;\nmso-header-margin:.51in;\nmso-footer-margin:.51in;\nmso-horizontal-page-align:center;\n}\ntd{font-size:12pt;text-align:center;}\ntd.bb{border:0.5pt solid black;height:22pt;}\n-->\n</style>\n<!--[if gte mso 9]><xml>\n<x:ExcelWorkbook>\n<x:ExcelWorksheets>\n<x:ExcelWorksheet>\n<x:Name>管材发货登记表</x:Name>\n<x:WorksheetOptions>\n<x:DefaultRowHeight>285</x:DefaultRowHeight>\n<x:Print>\n<x:ValidPrinterInfo/>\n<x:Scale>100</x:Scale>\n<x:PaperSizeIndex>9</x:PaperSizeIndex>\n<x:HorizontalResolution>200</x:HorizontalResolution>\n<x:VerticalResolution>200</x:VerticalResolution>\n</x:Print>\n<x:ShowPageBreakZoom/>\n<x:PageBreakZoom>100</x:PageBreakZoom>\n<x:Selected/>\n<x:Panes>\n<x:Pane>\n<x:Number>3</x:Number>\n<x:ActiveRow>0</x:ActiveRow>\n<x:ActiveCol>0</x:ActiveCol>\n</x:Pane>\n</x:Panes>\n</x:WorksheetOptions>\n"
        + "<x:PageBreaks><x:RowBreaks>" + "<x:RowBreak><x:Row><%=this.Rows+10 %></x:Row></x:RowBreak>" + "</x:RowBreaks></x:PageBreaks>" + "</x:ExcelWorksheet>\n</x:ExcelWorkbook>\n</xml><![endif]-->\n</head>\n<body>"
        + code.replace(/<\/TBODY>/img, "<tr><td width=50></td><td width=50></td><td width=50></td><td width=50></td><td width=50></td><td width=50></td><td width=50></td><td width=50></td><td width=50></td><td width=50></td><td width=50></td><td width=50></td></tr></TBODY>") + "</body></html>";
        
        form2.submit();
    }

</script>
</body>
</html>
